<!DOCTYPE html>
<html lang="zh-cn">
<head>
   
    <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=KOZafwuaDasEedEenI5aTy8aXH0epbm6VUJ0v3vsT_Q1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/ThinkInside/bundle-ThinkInside.css?v=RRjf6pEarGnbXZ86qxNycPfQivwSKWRa4heYLB15rVE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/428549.css?v=%2fam3bBTkW5NBWhBE%2fD0lcyJv5UM%3d"/>

</head>
<body>
<a name="top"></a>

<div id="page_begin_html"></div><script>load_page_begin_html();</script>

<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9310209.html">HBase（五）HBase的API操作</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><h2>一、项目环境搭建</h2>
<p>新建&nbsp;<strong>Maven</strong><strong>&nbsp;</strong><strong>Project，新建项目后在&nbsp;pom.xml 中添加依赖：</strong></p>
<div class="cnblogs_code">
<pre>&lt;dependency&gt;
&lt;groupId&gt;org.apache.hbase&lt;/groupId&gt;
&lt;artifactId&gt;hbase-server&lt;/artifactId&gt;
&lt;version&gt;<span style="color: #800080;">1.2</span>.<span style="color: #800080;">6</span>&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.hbase&lt;/groupId&gt;
&lt;artifactId&gt;hbase-client&lt;/artifactId&gt;
&lt;version&gt;<span style="color: #800080;">1.2</span>.<span style="color: #800080;">6</span>&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;jdk.tools&lt;/groupId&gt;
&lt;artifactId&gt;jdk.tools&lt;/artifactId&gt;
&lt;version&gt;<span style="color: #800080;">1.8</span>&lt;/version&gt;
&lt;scope&gt;system&lt;/scope&gt;
&lt;systemPath&gt;${JAVA_HOME}/lib/tools.jar&lt;/systemPath&gt;
&lt;/dependency&gt;</pre>
</div>
<h2>二、HBase API操作表和数据</h2>
<div class="Section12">
<p>注意，这部分的学习内容，我们先学习使用老版本的&nbsp;API，接着再写出新版本的&nbsp;API 调用方式。因为在企业中，有些时候我们需要一些过时的&nbsp;API 来提供更好的兼容性。</p>
<h3 class="16">1、获取<strong>Configuration</strong>对象</h3>
</div>
<div class="cnblogs_code">
<pre>public static Configuration conf; <br />  static{
      //使用 HBaseConfiguration 的单例方法实例化
      conf = HBaseConfiguration.create();
      conf.set("hbase.zookeeper.quorum", "node21,node22,node23");
      conf.set("hbase.zookeeper.property.clientPort", "2181");
  }</pre>
</div>
<div class="Section13">
<p class="16">2) 判断表是否存在：</p>
<div class="cnblogs_code">
<pre>public static boolean isTableExist(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{<br />
  //在HBase中管理、访问表需要先创建 HBaseAdmin 对象
  //Connection connection = ConnectionFactory.createConnection(conf);
  //HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); <br /><br />  HBaseAdmin admin = new HBaseAdmin(conf);
  return admin.tableExists(tableName);
}</pre>
</div>
<p class="16">3)&nbsp;创建表</p>
<div class="cnblogs_code">
<pre>public static void createTable(String tableName, String... columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
<br />HBaseAdmin admin = new HBaseAdmin(conf);
//判断表是否存在<br />if(isTableExist(tableName)){
   System.out.println("表" + tableName + "已存在");
   //System.exit(0);
}else{
   //创建表属性对象,表名需要转字节
   HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
   //创建多个列族
   for(String cf : columnFamily){<br />      descriptor.addFamily(new HColumnDescriptor(cf));
   }
//根据对表的配置，创建表
admin.createTable(descriptor);
System.out.println("表" + tableName + "创建成功！");
  }
}</pre>
</div>
</div>
<div class="Section14">
<p class="16">4)&nbsp;删除表</p>
<div class="cnblogs_code">
<pre>public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{<br />
HBaseAdmin admin = new HBaseAdmin(conf); <br />if(isTableExist(tableName)){
   admin.disableTable(tableName); <br />   admin.deleteTable(tableName);<br />   System.out.println("表" + tableName + "删除成功！");
}else{
    System.out.println("表" + tableName + "不存在！");
    }
}</pre>
</div>
<p class="16">5)&nbsp;向表中插入数据</p>
<div class="cnblogs_code">
<pre>public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException{<br />
//创建 HTable 对象
HTable hTable = new HTable(conf, tableName);
//向表中插入数据
Put put = new Put(Bytes.toBytes(rowKey));
//向 Put 对象中组装数据
put.add(Bytes.toBytes(columnFamily),<br />Bytes.toBytes(column), <br />Bytes.toBytes(value));
hTable.put(put);<br />hTable.close();
System.out.println("插入数据成功");
}</pre>
</div>
</div>
<div class="Section15">
<p class="16">6)&nbsp;删除多行数据</p>
<div class="cnblogs_code">
<pre>public static void deleteMultiRow(String tableName, String... rows) throws IOException{ <br /><br />HTable hTable = new HTable(conf, tableName);<br />List&lt;Delete&gt; deleteList = new ArrayList&lt;Delete&gt;(); <br />for(String row : rows){
    Delete delete = new Delete(Bytes.toBytes(row));<br />    deleteList.add(delete);
    }
hTable.delete(deleteList); <br />hTable.close();
}</pre>
</div>
<p class="16">7) 得到所有数据</p>
</div>
<div class="cnblogs_code">
<pre>public static void getAllRows(String tableName) throws IOException{ <br /><br />HTable hTable = new HTable(conf, tableName);
//得到用于扫描 region 的对象
Scan scan = new Scan();
//使用 HTable 得到 resultcanner 实现类的对象
ResultScanner resultScanner = hTable.getScanner(scan); <br />for(Result result : resultScanner){
    Cell[] cells = result.rawCells();<br />    for(Cell cell : cells){
       //得到 rowkey
       System.out.println("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
       //得到列族
       System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
       System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
       System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
       }
    }
}</pre>
</div>
<div class="Section16">
<p class="16">8) 得到某一行所有数据</p>
<div class="cnblogs_code">
<pre>public static void getRow(String tableName, String rowKey) throws IOException{ <br /><br />HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
//get.setMaxVersions();显示所有版本
//get.setTimeStamp();显示指定时间戳的版本<br />Result result = table.get(get);
for(Cell cell : result.rawCells()){
    System.out.println("行键:" + Bytes.toString(result.getRow()));
    System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
    System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
    System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
    System.out.println("时间戳:" + cell.getTimestamp());
   }
}</pre>
</div>
<p class="16">9)&nbsp;获取某一行指定&ldquo;列族<strong>:</strong>列&rdquo;的数据</p>
<div class="cnblogs_code">
<pre>public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException{<br />
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); <br />Result result = table.get(get);
for(Cell cell : result.rawCells()){
  System.out.println("行键:" + Bytes.toString(result.getRow()));
  System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));<br />  System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
  System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
  }
}</pre>
</div>
</div>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: #000000;">import java.io.IOException;
import java.util.Date;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import com.study.hbase.service.HBaseUtils;

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HBaseUtilsImpl implements HBaseUtils {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        getConnection();
        getAdmin();
        
        HBaseUtilsImpl hbu </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> HBaseUtilsImpl();
        
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;">hbu.getAllTables();
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;">hbu.descTable("people");
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;">String[] infos = {"info","family"};
        </span><span style="color: #008000;">//</span><span style="color: #008000;">hbu.createTable("people", infos);
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;">String[] add = {"cs1","cs2"};
        </span><span style="color: #008000;">//</span><span style="color: #008000;">String[] remove = {"cf1","cf2"};
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;">HColumnDescriptor hc = new HColumnDescriptor("sixsixsix");
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;">hbu.modifyTable("stu",hc);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">hbu.getAllTables();</span>
<span style="color: #000000;">
        
        hbu.putData(</span><span style="color: #800000;">"</span><span style="color: #800000;">huoying</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">rk001</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">cs2</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">aobama</span><span style="color: #800000;">"</span>,<span style="color: #0000ff;">new</span><span style="color: #000000;"> Date().getTime());
        hbu.getAllTables();
        
        conn.close();
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取连接</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> Connection getConnection() {
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建一个可以用来管理hbase配置信息的conf对象</span>
        Configuration conf =<span style="color: #000000;"> HBaseConfiguration.create();
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置当前的程序去寻找的hbase在哪里</span>
        conf.<span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
            e.printStackTrace();
        }
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> conn;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取管理员对象</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> Admin getAdmin() {
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            admin </span>=<span style="color: #000000;"> conn.getAdmin();
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
            e.printStackTrace();
        }
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> admin;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 查询所有表</span>
<span style="color: #000000;">    @Override
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> getAllTables() throws Exception {
        </span><span style="color: #008000;">//</span><span style="color: #008000;">获取列簇的描述信息</span>
        HTableDescriptor[] listTables =<span style="color: #000000;"> admin.listTables();
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (HTableDescriptor listTable : listTables) {
            </span><span style="color: #008000;">//</span><span style="color: #008000;">转化为表名</span>
            String tbName =<span style="color: #000000;"> listTable.getNameAsString();
            </span><span style="color: #008000;">//</span><span style="color: #008000;">获取列的描述信息</span>
            HColumnDescriptor[] columnFamilies =<span style="color: #000000;"> listTable.getColumnFamilies();
            System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">tableName:</span><span style="color: #800000;">"</span>+<span style="color: #000000;">tbName);
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(HColumnDescriptor columnFamilie : columnFamilies) {
                </span><span style="color: #008000;">//</span><span style="color: #008000;">获取列簇的名字</span>
                String columnFamilyName =<span style="color: #000000;"> columnFamilie.getNameAsString();
                System.</span><span style="color: #0000ff;">out</span>.print(<span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span>+<span style="color: #800000;">"</span><span style="color: #800000;">columnFamilyName:</span><span style="color: #800000;">"</span>+<span style="color: #000000;">columnFamilyName);
            }
             System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println();
         }
 
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建表，传参，表名和列簇的名字</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> createTable(String tableName, String[] family) throws Exception {
         
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否存在</span>
         <span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table已经存在！</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             </span><span style="color: #008000;">//</span><span style="color: #008000;">表的列簇示例</span>
             HTableDescriptor htd = <span style="color: #0000ff;">new</span><span style="color: #000000;"> HTableDescriptor(name);
             </span><span style="color: #008000;">//</span><span style="color: #008000;">向列簇中添加列的信息</span>
             <span style="color: #0000ff;">for</span><span style="color: #000000;">(String str : family) {
                 HColumnDescriptor hcd </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> HColumnDescriptor(str);
                 htd.addFamily(hcd);
             }
             </span><span style="color: #008000;">//</span><span style="color: #008000;">创建表</span>
<span style="color: #000000;">             admin.createTable(htd);
             </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否创建成功</span>
             <span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
                 System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table创建成功</span><span style="color: #800000;">"</span><span style="color: #000000;">);
             }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                 System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table创建失败</span><span style="color: #800000;">"</span><span style="color: #000000;">);
             }
         }    
         
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建表，传参:封装好的多个列簇</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> createTable(HTableDescriptor htds) throws Exception {
         </span><span style="color: #008000;">//</span><span style="color: #008000;">获得表的名字</span>
         String tbName =<span style="color: #000000;"> htds.getNameAsString();
         
         admin.createTable(htds);
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建表，传参，表名和封装好的多个列簇</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> createTable(String tableName, HTableDescriptor htds) throws Exception {
 
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table已经存在！</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             admin.createTable(htds);
             boolean flag </span>=<span style="color: #000000;"> admin.tableExists(name);
             System.</span><span style="color: #0000ff;">out</span>.println(flag ? <span style="color: #800000;">"</span><span style="color: #800000;">创建成功</span><span style="color: #800000;">"</span> : <span style="color: #800000;">"</span><span style="color: #800000;">创建失败</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }
         
     }
 
     
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 查看表的列簇属性</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> descTable(String tableName) throws Exception {
         </span><span style="color: #008000;">//</span><span style="color: #008000;">转化为表名</span>
         TableName name =<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否存在</span>
         <span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             </span><span style="color: #008000;">//</span><span style="color: #008000;">获取表中列簇的描述信息</span>
             HTableDescriptor tableDescriptor =<span style="color: #000000;"> admin.getTableDescriptor(name);
             </span><span style="color: #008000;">//</span><span style="color: #008000;">获取列簇中列的信息</span>
             HColumnDescriptor[] columnFamilies =<span style="color: #000000;"> tableDescriptor.getColumnFamilies();
             </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(HColumnDescriptor columnFamily : columnFamilies) {
                 System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(columnFamily);
             }
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }
         
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 判断表存在不存在</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> boolean existTable(String tableName) throws Exception {
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> admin.tableExists(name);
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> disable表</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> disableTable(String tableName) throws Exception {
         
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.isTableEnabled(name)) {
                 admin.disableTable(name);
             }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                 System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不是活动状态</span><span style="color: #800000;">"</span><span style="color: #000000;">);
             }
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }
             
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> drop表</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> dropTable(String tableName) throws Exception {
         </span><span style="color: #008000;">//</span><span style="color: #008000;">转化为表名</span>
         TableName name =<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否存在</span>
         <span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否处于可用状态</span>
             boolean tableEnabled =<span style="color: #000000;"> admin.isTableEnabled(name);
             
             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(tableEnabled) {
                 </span><span style="color: #008000;">//</span><span style="color: #008000;">使表变成不可用状态</span>
<span style="color: #000000;">                 admin.disableTable(name);
             }
             </span><span style="color: #008000;">//</span><span style="color: #008000;">删除表</span>
<span style="color: #000000;">             admin.deleteTable(name);
             </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否存在</span>
             <span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
                 System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">删除失败</span><span style="color: #800000;">"</span><span style="color: #000000;">);
             }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                 System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">删除成功</span><span style="color: #800000;">"</span><span style="color: #000000;">);
             }
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         } 
         
         
     }
     
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 修改表(增加和删除)</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> modifyTable(String tableName) throws Exception {
         </span><span style="color: #008000;">//</span><span style="color: #008000;">转化为表名</span>
         TableName name =<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否存在</span>
         <span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否可用状态</span>
             boolean tableEnabled =<span style="color: #000000;"> admin.isTableEnabled(name);
             
             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(tableEnabled) {
                 </span><span style="color: #008000;">//</span><span style="color: #008000;">使表变成不可用</span>
<span style="color: #000000;">                 admin.disableTable(name);
             }
             </span><span style="color: #008000;">//</span><span style="color: #008000;">根据表名得到表</span>
             HTableDescriptor tableDescriptor =<span style="color: #000000;"> admin.getTableDescriptor(name);
             </span><span style="color: #008000;">//</span><span style="color: #008000;">创建列簇结构对象</span>
             HColumnDescriptor columnFamily1 = <span style="color: #0000ff;">new</span> HColumnDescriptor(<span style="color: #800000;">"</span><span style="color: #800000;">cf1</span><span style="color: #800000;">"</span><span style="color: #000000;">.getBytes());
             HColumnDescriptor columnFamily2 </span>= <span style="color: #0000ff;">new</span> HColumnDescriptor(<span style="color: #800000;">"</span><span style="color: #800000;">cf2</span><span style="color: #800000;">"</span><span style="color: #000000;">.getBytes());
             
             tableDescriptor.addFamily(columnFamily1);
             tableDescriptor.addFamily(columnFamily2);
             </span><span style="color: #008000;">//</span><span style="color: #008000;">替换该表所有的列簇</span>
<span style="color: #000000;">             admin.modifyTable(name, tableDescriptor);
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         } 
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 修改表(增加和删除)</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> modifyTable(String tableName, String[] addColumn, String[] removeColumn) throws Exception {
         </span><span style="color: #008000;">//</span><span style="color: #008000;">转化为表名</span>
         TableName name =<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否存在</span>
         <span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             </span><span style="color: #008000;">//</span><span style="color: #008000;">判断表是否可用状态</span>
             boolean tableEnabled =<span style="color: #000000;"> admin.isTableEnabled(name);
             
             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(tableEnabled) {
                 </span><span style="color: #008000;">//</span><span style="color: #008000;">使表变成不可用</span>
<span style="color: #000000;">                 admin.disableTable(name);
             }
             </span><span style="color: #008000;">//</span><span style="color: #008000;">根据表名得到表</span>
             HTableDescriptor tableDescriptor =<span style="color: #000000;"> admin.getTableDescriptor(name);
             </span><span style="color: #008000;">//</span><span style="color: #008000;">创建列簇结构对象，添加列</span>
             <span style="color: #0000ff;">for</span><span style="color: #000000;">(String add : addColumn) {
                 HColumnDescriptor addColumnDescriptor </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> HColumnDescriptor(add);
                 tableDescriptor.addFamily(addColumnDescriptor);
             }
             </span><span style="color: #008000;">//</span><span style="color: #008000;">创建列簇结构对象，删除列</span>
             <span style="color: #0000ff;">for</span><span style="color: #000000;">(String remove : removeColumn) {
                 HColumnDescriptor removeColumnDescriptor </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> HColumnDescriptor(remove);
                 tableDescriptor.removeFamily(removeColumnDescriptor.getName());
             }
             
             admin.modifyTable(name, tableDescriptor);
             
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         } 
         
     }
 
     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> modifyTable(String tableName, HColumnDescriptor hcds) throws Exception {
         </span><span style="color: #008000;">//</span><span style="color: #008000;">转化为表名</span>
         TableName name =<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #008000;">//</span><span style="color: #008000;">根据表名得到表</span>
         HTableDescriptor tableDescriptor =<span style="color: #000000;"> admin.getTableDescriptor(name);
         </span><span style="color: #008000;">//</span><span style="color: #008000;">获取表中所有的列簇信息</span>
         HColumnDescriptor[] columnFamilies =<span style="color: #000000;"> tableDescriptor.getColumnFamilies();
         
         boolean flag </span>= <span style="color: #0000ff;">false</span><span style="color: #000000;">;
         </span><span style="color: #008000;">//</span><span style="color: #008000;">判断参数中传入的列簇是否已经在表中存在</span>
         <span style="color: #0000ff;">for</span><span style="color: #000000;">(HColumnDescriptor columnFamily : columnFamilies) {
             </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(columnFamily.equals(hcds)) {
                 flag </span>= <span style="color: #0000ff;">true</span><span style="color: #000000;">;
             }
         }    
         </span><span style="color: #008000;">//</span><span style="color: #008000;">存在提示，不存在直接添加该列簇信息</span>
         <span style="color: #0000ff;">if</span><span style="color: #000000;">(flag) {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">该列簇已经存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             tableDescriptor.addFamily(hcds);
             admin.modifyTable(name, tableDescriptor);
         }
         
     }
 
     
     </span><span style="color: #008000;">/*</span><span style="color: #008000;">*添加数据
     *tableName:    表明
     *rowKey:    行键
     *familyName:列簇
     *columnName:列名
     *value:        值
     </span><span style="color: #008000;">*/</span><span style="color: #000000;">
     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> putData(String tableName, String rowKey, String familyName, String columnName, String value)
             throws Exception {
         </span><span style="color: #008000;">//</span><span style="color: #008000;">转化为表名</span>
         TableName name =<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #008000;">//</span><span style="color: #008000;">添加数据之前先判断表是否存在，不存在的话先创建表</span>
         <span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
         
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             </span><span style="color: #008000;">//</span><span style="color: #008000;">根据表明创建表结构</span>
             HTableDescriptor tableDescriptor = <span style="color: #0000ff;">new</span><span style="color: #000000;"> HTableDescriptor(name);
             </span><span style="color: #008000;">//</span><span style="color: #008000;">定义列簇的名字</span>
             HColumnDescriptor columnFamilyName = <span style="color: #0000ff;">new</span><span style="color: #000000;"> HColumnDescriptor(familyName);
             tableDescriptor.addFamily(columnFamilyName);
             admin.createTable(tableDescriptor);
         
         }
 
         Table table </span>=<span style="color: #000000;"> conn.getTable(name);
         Put put </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Put(rowKey.getBytes());
         
         put.addColumn(familyName.getBytes(), columnName.getBytes(), value.getBytes());
         table.put(put);
 
     }
 
     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> putData(String tableName, String rowKey, String familyName, String columnName, String value,
             </span><span style="color: #0000ff;">long</span><span style="color: #000000;"> timestamp) throws Exception {
 
         </span><span style="color: #008000;">//</span><span style="color: #008000;"> 转化为表名</span>
         TableName name =<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加数据之前先判断表是否存在，不存在的话先创建表</span>
         <span style="color: #0000ff;">if</span><span style="color: #000000;"> (admin.tableExists(name)) {
 
         } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             </span><span style="color: #008000;">//</span><span style="color: #008000;"> 根据表明创建表结构</span>
             HTableDescriptor tableDescriptor = <span style="color: #0000ff;">new</span><span style="color: #000000;"> HTableDescriptor(name);
             </span><span style="color: #008000;">//</span><span style="color: #008000;"> 定义列簇的名字</span>
             HColumnDescriptor columnFamilyName = <span style="color: #0000ff;">new</span><span style="color: #000000;"> HColumnDescriptor(familyName);
             tableDescriptor.addFamily(columnFamilyName);
             admin.createTable(tableDescriptor);
 
         }
 
         Table table </span>=<span style="color: #000000;"> conn.getTable(name);
         Put put </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Put(rowKey.getBytes());
 
         </span><span style="color: #008000;">//</span><span style="color: #008000;">put.addColumn(familyName.getBytes(), columnName.getBytes(), value.getBytes());</span>
<span style="color: #000000;">         put.addImmutable(familyName.getBytes(), columnName.getBytes(), timestamp, value.getBytes());
         table.put(put);
 
     }
 
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 根据rowkey查询数据</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Result getResult(String tableName, String rowKey) throws Exception {
         
         Result result;
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             Get </span><span style="color: #0000ff;">get</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Get(rowKey.getBytes());
             result </span>= table.<span style="color: #0000ff;">get</span>(<span style="color: #0000ff;">get</span><span style="color: #000000;">);
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             result </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
         }
         
         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 根据rowkey查询数据</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Result getResult(String tableName, String rowKey, String familyName) throws Exception {
         Result result;
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             Get </span><span style="color: #0000ff;">get</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Get(rowKey.getBytes());
             </span><span style="color: #0000ff;">get</span><span style="color: #000000;">.addFamily(familyName.getBytes());
             result </span>= table.<span style="color: #0000ff;">get</span>(<span style="color: #0000ff;">get</span><span style="color: #000000;">);
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             result </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
         }
         
         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 根据rowkey查询数据</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Result getResult(String tableName, String rowKey, String familyName, String columnName) throws Exception {
         
         Result result;
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             Get </span><span style="color: #0000ff;">get</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Get(rowKey.getBytes());
             </span><span style="color: #0000ff;">get</span><span style="color: #000000;">.addColumn(familyName.getBytes(), columnName.getBytes());
             result </span>= table.<span style="color: #0000ff;">get</span>(<span style="color: #0000ff;">get</span><span style="color: #000000;">);
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             result </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
         }
         
         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 查询指定version</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Result getResultByVersion(String tableName, String rowKey, String familyName, String columnName,
             </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> versions) throws Exception {
         
         Result result;
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             Get </span><span style="color: #0000ff;">get</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Get(rowKey.getBytes());
             </span><span style="color: #0000ff;">get</span><span style="color: #000000;">.addColumn(familyName.getBytes(), columnName.getBytes());
             </span><span style="color: #0000ff;">get</span><span style="color: #000000;">.setMaxVersions(versions);
             result </span>= table.<span style="color: #0000ff;">get</span>(<span style="color: #0000ff;">get</span><span style="color: #000000;">);
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             result </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
         }
         
         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> scan全表数据</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ResultScanner getResultScann(String tableName) throws Exception {
         
         ResultScanner result;
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();
             result </span>=<span style="color: #000000;"> table.getScanner(scan);
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             result </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
         }
         
         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> scan全表数据</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ResultScanner getResultScann(String tableName, Scan scan) throws Exception {
         
         ResultScanner result;
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             result </span>=<span style="color: #000000;"> table.getScanner(scan);
             
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             result </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
         }
         
         </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 删除数据（指定的列）</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> deleteColumn(String tableName, String rowKey) throws Exception {
         
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             Delete delete </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Delete(rowKey.getBytes());
             table.delete(delete);
         
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }
         
         
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 删除数据（指定的列）</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> deleteColumn(String tableName, String rowKey, String falilyName) throws Exception {
         
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             Delete delete </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Delete(rowKey.getBytes());
             delete.addFamily(falilyName.getBytes());
             table.delete(delete);
         
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }
         
     }
 
     </span><span style="color: #008000;">//</span><span style="color: #008000;"> 删除数据（指定的列）</span>
<span style="color: #000000;">     @Override
     </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> deleteColumn(String tableName, String rowKey, String falilyName, String columnName) throws Exception {
         TableName name </span>=<span style="color: #000000;"> TableName.valueOf(tableName);
         </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(admin.tableExists(name)) {
             Table table </span>=<span style="color: #000000;"> conn.getTable(name);
             Delete delete </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Delete(rowKey.getBytes());
             delete.addColumn(falilyName.getBytes(), columnName.getBytes());
             table.delete(delete);
         
         }</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
             System.</span><span style="color: #0000ff;">out</span>.println(<span style="color: #800000;">"</span><span style="color: #800000;">table不存在</span><span style="color: #800000;">"</span><span style="color: #000000;">);
         }
     }
 
 }</span></pre>
</div>
<p>&nbsp;</p></div>

</body>
</html>
